package com.swt.gap.controller;

import com.swt.gap.model.entity.EventElement;
import com.swt.gap.service.EventElementService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@Api(tags = "自定义要素类型")
@RestController
@RequestMapping("/event-element")
@Slf4j
public class EventElementController {

    private final EventElementService eventElementService;

    public EventElementController(EventElementService eventElementService) {
        this.eventElementService = eventElementService;
    }


    @PostMapping
    @ApiOperation(value = "新增要素类型", notes = "新增要素类型")
    @ApiImplicitParam(name = "eventElement", dataType = "EventElement",paramType = "body", value = "自定义要素")
    @ApiResponses({@ApiResponse(code = 201, message = "添加成功"),
            @ApiResponse(code = 409, message = "已重名"),
            @ApiResponse(code = 400, message = "非法请求")})
    public ResponseEntity addElement(@RequestBody EventElement eventElement){
        Integer userId = UserLoginUtils.getUserId();
        eventElement.setCreatedBy(userId);
        //对要素名的校验
        String element = eventElement.getElement();
        if (StringUtils.isNotBlank(element)) {
            element = element.trim();
            if (element.length() > 10) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称长度不能超过10");
            }
            eventElement.setElement(element);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称不能为空");
        }
        //没提取到内容时给定默认表头的新增自定义要素，不操作数据库
        if (eventElement.getEntityId() == null || eventElement.getEntityId() == 0) {
            eventElement.setElementId((int) System.currentTimeMillis());
            return ResponseEntity.status(HttpStatus.CREATED).body(eventElement);
        }
        //判断重名
        Boolean isRepeat = eventElementService.judgeElement(eventElement.getEntityId(), userId, element, null);
        if (isRepeat) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("要素类型已存在");
        }
        eventElementService.addElement(eventElement);
        return ResponseEntity.status(HttpStatus.CREATED).body(eventElement);
    }


    @DeleteMapping("/{elementId}")
    @ApiOperation(value = "删除自定义要素类型", notes = "删除自定义要素类型")
    @ApiImplicitParam(name = "elementId", dataType = "int",paramType = "path", value = "自定义要素Id")
    @ApiResponses({@ApiResponse(code = 200, message = "ok")})
    public ResponseEntity deleteElement(@PathVariable Integer elementId){
        //没提取到内容时给定默认表头的删除自定义要素，不操作数据库
        if (elementId == null || elementId == 0) {
            return ResponseEntity.ok("删除成功");
        }
        eventElementService.deleteElement(elementId);
        return ResponseEntity.ok("删除成功");
    }


    @PutMapping("/{elementId}")
    @ApiOperation(value = "编辑自定义要素", notes = "编辑自定义要素")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "elementId", dataType = "int",paramType = "path", value = "自定义要素Id"),
            @ApiImplicitParam(name = "eventElement", dataType = "EventElement",paramType = "body", value = "自定义要素")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok"),
            @ApiResponse(code = 409, message = "已重名"),
            @ApiResponse(code = 400, message = "非法请求")})
    public ResponseEntity updateElement(@PathVariable Integer elementId, @RequestBody EventElement eventElement){
        Integer userId = UserLoginUtils.getUserId();
        eventElement.setElementId(elementId);
        //对要素名的校验
        String element = eventElement.getElement();
        if (StringUtils.isNotBlank(element)) {
            element = element.trim();
            if (element.length() > 10) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称长度不能超过10");
            }
            eventElement.setElement(element);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("名称不能为空");
        }
        //没提取到内容时给定默认表头的编辑自定义要素，不操作数据库
        if (eventElement.getEntityId() == null || eventElement.getEntityId() == 0) {
            return ResponseEntity.ok(eventElement);
        }
        //判断重名
        Boolean isRepeat = eventElementService.judgeElement(eventElement.getEntityId(), userId, element, elementId);
        if (isRepeat) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("要素类型已存在");
        }
        eventElementService.updateElement(eventElement);
        return ResponseEntity.ok(eventElement);
    }




}
